package com.edu.bean;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * @Author:zyn
 * @Date:2025/11/7--11:10:13
 */
public class MyIoc {

    //提供map集合用于装被管理的对象
    private Map<Object,Object> box;

    //用于读取配置文件
    private Properties properties;

    //用于装对象的属性
    private Properties objectProperty;

    public MyIoc(String path){
        box=new HashMap<>();
        objectProperty=new Properties();
        readProperties(path);
    }

    public Object getBean(String beanId){
        return box.get(beanId);
    }

    //读取配置文件
    public void readProperties(String path){
        properties=new Properties();
        InputStream inputStream = MyIoc.class.getClassLoader().getResourceAsStream(path);
        try {
            properties.load(inputStream);
            createObject();
        } catch (IOException e) {
            System.out.println("该配置文件不存在");
            e.printStackTrace();
        }
    }

    //创建要被管理的对象
    public void createObject(){
        //获取properties里的所有key
        Set<String> beanIds = properties.stringPropertyNames();
        for (String beanId : beanIds) {
            //通过key获取对应的value
            String className = properties.getProperty(beanId);
            try {
                //判断key里是否有"."
                if(beanId.contains(".")){
                    objectProperty.setProperty(beanId,className);
                    continue;
                }
                Class clazz=Class.forName(className);
                Object o = clazz.newInstance();
                box.put(beanId,o);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        serBeanProperty();
    }
    //给对象属性赋值
    public void serBeanProperty(){
        //获取objectProperty里的所有key
        Set<String> keys = objectProperty.stringPropertyNames();
        for (String key : keys) {
            int index=key.indexOf(".");
            String beanId=key.substring(0,index);
            Object o=box.get(beanId);
            if(o == null){
                System.out.println(beanId+"的对象不存在");
                continue;
            }
            String filedName=key.substring(index+1);
            Class clazz=o.getClass();
            try {
                Field field = clazz.getDeclaredField(filedName);
                String value=objectProperty.getProperty(key);
                field.setAccessible(true);
                Class targetClass=field.getType();
                field.set(o,typeTransForm(value,targetClass));
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

        }
    }

    //类型转换
    public Object typeTransForm(String str,Class targetClass){
        if(targetClass.getSimpleName().equals("int")){
            int i = Integer.parseInt(str);
            return i;
        }
        return str;
    }
}
